package com.team.ai.service;



//biedong

import cn.dev33.satoken.annotation.SaCheckLogin;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;

@SaCheckLogin
@Component
@Service
public class GptService {
    public String sendPost(String content) throws IOException {
////        Integer userId = StpUtil.getLoginIdAsInt();
////        contentMapper.insert(content,content,userId);
//        String ans = "";
////定义接收数据
//        JSONObject result = new JSONObject();
//        String url = "https://api.openai.com/v1/chat/completions";
//        HttpPost httpPost = new HttpPost(url);
//        httpPost.setHeader("Authorization", "Bearer ak-1587007840");
//        httpPost.setHeader("Content-Type","application/json");
//        CloseableHttpClient client = HttpClients.createDefault();
//
////请求参数转JOSN字符串
//        String payLoad ="{\"messages\":[{\"role\":\"user\",\"content\":"+content+"}],\"model\":\"gpt-3.5-turbo\",\"temperature\": 0.7}";
//        StringEntity entity = new StringEntity(payLoad, "UTF-8");
//        entity.setContentEncoding("UTF-8");
//        entity.setContentType("application/json");
//
//        httpPost.setEntity(entity);
//        try {
//            HttpResponse response = client.execute(httpPost);
//            if (response.getStatusLine().getStatusCode() == 200) {
////                System.out.println(response.getEntity());
////                System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));
//                ans = EntityUtils.toString(response.getEntity(), "UTF-8");
//                System.out.println(ans);
////                result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
//            }else{
//                System.out.println("~~~~~~~"+response.getStatusLine().getStatusCode());
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//            result.put("error", "连接错误！");
//        }
////关闭连接
//        try {
//            client.close();
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//
//        }
//        return ans;

//        String pro = "127.0.0.1";//本机地址
//        int pro1 = 57347; //代理端口号
        //创建一个 HttpHost 实例，这样就设置了代理服务器的主机和端口。
//        HttpHost httpHost = new HttpHost(pro, pro1);
        //创建一个 RequestConfig 对象，然后使用 setProxy() 方法将代理 httpHost 设置进去。
//        RequestConfig build = RequestConfig.custom().setProxy(httpHost).build();

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//        HttpPost post = new HttpPost("https://api.openai.com/v1/chat/completions");
        HttpPost post = new HttpPost("http://124.70.129.252:3000/api/openai/v1/chat/completions");
        post.setHeader("Content-Type","application/json");
        post.setHeader("Authorization", "Bearer nk-a1587007840");//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
        //将 build 配置设置到 post 请求中包括先前指定的代理设置。
//        post.setConfig(build);
        String paramJson ="{\"model\": \"gpt-3.5-turbo\",\"messages\": [{\"role\": \"user\", \"content\": \""+content+"\"}]}";
        StringEntity stringEntity = new StringEntity(paramJson, ContentType.create("text/json", "UTF-8"));
        post.setEntity(stringEntity);

        CloseableHttpResponse response = httpClient.execute(post);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String res = EntityUtils.toString(response.getEntity());
            int idx = res.indexOf("\"content\"");
            int idx1 = res.indexOf("\"}");
            return res.substring(idx+11,idx1);
        } else {
            System.out.println(response.getStatusLine().getStatusCode());
            return "fail";
        }


    }
}
